package com.team7.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.order.dao.CourseDao;
import com.team7.order.dao.PayLogDao;
import com.team7.order.entity.Course;
import com.team7.order.entity.Order;
import com.team7.order.dao.OrderDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.order.entity.PayLog;
import com.team7.order.utils.CommonResult;
import com.team7.order.vo.CourseDetailsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-20*/


@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements com.team7.order.service.OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private PayLogDao payLogDao;
    @Resource
    private CourseDao courseDao;
    //  查询订单的全部信息(order表和PayLog表)
    public CommonResult getAllOrder(Integer page, Integer limit, Order order) {
        Page<Order> orderPage = new Page<>(page, limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(order.getMobile())) {
            wrapper.eq("tor.mobile", order.getMobile());
        }
        if (StringUtils.isNotEmpty(order.getOrderNo())) {
            wrapper.eq("tor.order_no", order.getOrderNo());
        }
        if (StringUtils.isNotEmpty(order.getTeacherName())) {
            wrapper.like("teacher_name", order.getTeacherName());
        }
        if (order.getStatus() != null) {
            wrapper.eq("tor.status", order.getStatus());
        }
        if (order.getPayType() != null) {
            wrapper.eq("tor.pay_type", order.getPayType());
        }
        if (StringUtils.isNotEmpty(order.getGmtCreate())) {
            wrapper.gt("tor.gmt_modified", order.getGmtCreate());
        }
        if (StringUtils.isNotEmpty(order.getGmtModified())) {
            wrapper.le("tor.gmt_modified", order.getGmtModified());
        }
        if (StringUtils.isNotEmpty(order.getCourseTitle())) {
            wrapper.like("course_title", order.getCourseTitle());
        }
        Page<Order> selectPage = orderDao.getAllOrder(orderPage, wrapper);
        return new CommonResult(2000, "查询成功", selectPage);
    }
    //两表联查
    @Override
    public CommonResult queryOrderAndPayLog(String id) {
        Order order = orderDao.queryOrderAndPayLog(id);
        return new CommonResult(2000,"查询成功",order);
    }
    //根据订单消息
    @Override
    public CommonResult queryOrderByPay() {
        List<Order> orders = orderDao.queryOrderByPay();
        return new CommonResult(2000,"查询成功",orders);
    }

    @Override
    @Transactional
    public CommonResult deleteOrder(String id) {
        Order order = orderDao.selectById(id);
        int i = orderDao.deleteById(id);
        QueryWrapper<PayLog> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order.getOrderNo());
        int del = payLogDao.deleteById(wrapper);
        if (del>0 && i>0){
            return new CommonResult(2000,"订单取消成功",null);
        }
        return new CommonResult(5000,"订单取消失败",null);
    }

    //    根据课程id和会员di查询支付状态
    @Override
    public CommonResult getOrderStatusByCourseIdAndHuiYuanId(String id, String HuiYuanId) {
        Order order = orderDao.getOrderStatusByCourseIdAndHuiYuanId(id, HuiYuanId);
        return new CommonResult(2000,"成功",order);
    }
    //    更改支付状态
    @Override
    public CommonResult updateZhiFuStatus(String id, String HuiYuanId) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        queryWrapper.eq("member_id",HuiYuanId);
        Order order = new Order();
        order.setStatus(1);
        orderDao.update(order, queryWrapper);

        return new CommonResult(2000,"成功",queryWrapper);
    }
    //    更改支付状态
    @Override
    public CommonResult updateStatus(String order_no) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = new Order();
        order.setStatus(1);
        orderDao.update(order,wrapper);
//        购买数量+1
//        查询当前订单信息
        QueryWrapper<Order> OrderWrapper = new QueryWrapper<>();
        OrderWrapper.eq("order_no",order_no);
        Order order1 = orderDao.selectOne(wrapper);
        CourseDetailsVo courseDetailsVo = courseDao.queryAllCourseAndTeacherName(order1.getCourseId());

    //更改
        long l = courseDetailsVo.getBuyCount() + 1;
        Course course = new Course();
        course.setBuyCount(l);
        course.setId(courseDetailsVo.getId());
        courseDao.updateById(course);
        return new CommonResult(2000,"成功",wrapper);
    }

    @Override
    public CommonResult queryCurrentOrderXinXi(String id, String HuiYuanId) {
        //根据课程id和会员查询当前订单信息
        QueryWrapper<Order> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("course_id",id);
        orderWrapper.eq("member_id",HuiYuanId);
        Order order = orderDao.selectOne(orderWrapper);
        return new CommonResult(2000,"查询成功",order);
    }
}
